package com.qjc.demo.data_structure.tree_demo;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 在一个公司的组织架构中，最高级别的我们称为总部，总部下可以有多个下级部门，比如销售部，财务部等，
 * 每个子部门下又可以有多个子部门，请设计一个或多个对象类，并且提供一个方法，可以遍历出指定部门下所有子部门和子子部门（无限往下）
 * @Author: qjc
 * @Date: 2020/3/19
 */
public class DeptTest {

    public static void main(String[] args) {
        Department department = new Department();
        department.setName("总部");

        List<Department> departmentList = new ArrayList<>();

        Department department1 = new Department();
        department1.setName("一级部门1");

        List<Department> departmentList1 = new ArrayList<>();
        Department department11 = new Department();
        department11.setName("二级部门11");
        departmentList1.add(department11);
        Department department12 = new Department();
        department12.setName("二级部门12");
        departmentList1.add(department12);
        Department department13 = new Department();
        department13.setName("二级部门13");
        departmentList1.add(department13);
        department1.setChildDepartMentList(departmentList1);

        departmentList.add(department1);

        Department department2 = new Department();
        department2.setName("一级部门2");
        List<Department> departmentList2 = new ArrayList<>();
        Department department21 = new Department();
        department21.setName("二级部门21");
        departmentList2.add(department21);
        Department department22 = new Department();
        department22.setName("二级部门22");
        departmentList2.add(department22);
        Department department23 = new Department();
        department23.setName("二级部门23");
        departmentList2.add(department23);
        department2.setChildDepartMentList(departmentList2);

        departmentList.add(department2);

        Department department3 = new Department();
        department3.setName("一级部门3");
        List<Department> departmentList3 = new ArrayList<>();
        Department department31 = new Department();
        department31.setName("二级部门31");
        departmentList3.add(department31);
        Department department32 = new Department();
        department32.setName("二级部门32");

        List<Department> departmentList321 = new ArrayList<>();
        Department department321 = new Department();
        department321.setName("三级部门321");
        departmentList321.add(department321);
        Department department322 = new Department();
        department322.setName("三级部门322");
        departmentList321.add(department322);
        department32.setChildDepartMentList(departmentList321);

        departmentList3.add(department32);

        department3.setChildDepartMentList(departmentList3);

        departmentList.add(department3);

        department.setChildDepartMentList(departmentList);

        getSonDept(department);


    }

    private static void getSonDept(Department department) {
        if (department != null) {
            String name = department.getName();
            List<Department> childDepartMentList = department.getChildDepartMentList();
            if (childDepartMentList == null || childDepartMentList.size() == 0) {
                // 子部门/子子部门
                System.err.println(name);
            } else {
                // 本部门
                System.err.println(name);
                for (Department department1 : childDepartMentList) {
                    getSonDept(department1);
                }
            }
        }
    }


}
